package com.ruoyi.base.service;

import com.ruoyi.base.enums.ExceptionEnum;
import com.ruoyi.base.exception.SysException;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;

/**
 * service基类
 *
 * @author lcf
 */
public class BaseService {
    /**
     * 抛出异常
     */
    protected SysException exception(ExceptionEnum e) {
        return new SysException(e);
    }

    /**
     * 抛出异常
     */
    protected SysException exception(String msg) {
        return new SysException(msg);
    }

    /**
     * 抛出异常
     */
    protected RuntimeException exception(String msg, Exception e) {
        return new RuntimeException(msg, e);
    }

    /**
     * 检查并返回
     */
    protected <T> T getOrThrow(Supplier<T> supplier, ExceptionEnum e) {
        return Optional.ofNullable(supplier.get()).orElseThrow(() -> exception(e));
    }

    protected <T> List<T> emptyList() {
        return Collections.emptyList();
    }
}
